package com.companyName.sample.service.business;

import com.companyName.sample.bean.dto.sys.*;
import com.companyName.sample.common.util.CommUtil;
import com.companyName.sample.common.util.PageInfo;
import com.companyName.sample.data.entity.*;
import com.companyName.sample.data.mapper.SysLogMapper;
import com.companyName.sample.data.mapper.UserMapper;
import com.companyName.sample.data.mapper.UserRoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guo.jun on 2016/4/11.
 */
@Component
public class UserBiz {
    private static final Logger logger = LoggerFactory.getLogger(UserBiz.class);

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;


    /**
     * 根据用户名查找user对象
     */
    public UserDto findUserByLoginName(String username) {
        UserPo userpo = userMapper.findUserByLoginName(username);
        if(userpo == null){
            return null;
        }else {
            UserDto userDto = new UserDto();
            CommUtil.copyProperties(userpo, userDto);
            return userDto;
        }
    }

    /**
     * 根据Id查找user对象
     */
    public UserDto findUserById(Long id) {
        UserPo userpo = userMapper.findUserById(id);
        UserDto userDto = new UserDto();
        CommUtil.copyProperties(userpo, userDto);
        return userDto;
    }


    public void findDataGrid(PageInfo pageInfo) {
        pageInfo.setRows(userMapper.findUserPageCondition(pageInfo));
        pageInfo.setTotal(userMapper.findUserPageCount(pageInfo));
    }


    public void addUser(UserVo userVo) {
        UserPo user = new UserPo();
        try {
            CommUtil.copyProperties(userVo, user);
        } catch (Exception e) {
            logger.error("类转换异常：{}", e);
            throw new RuntimeException("类型转换异常：{}", e);
        }
        userMapper.insert(user);

        Long id = user.getId();
        String[] roles = userVo.getRoleIds().split(",");
        UserRolePo userRolePo = new UserRolePo();

        for (String string : roles) {
            userRolePo.setUserId(id);
            userRolePo.setRoleId(Long.valueOf(string));
            userRoleMapper.insert(userRolePo);
        }
    }


    public void updateUserPwdById(Long userId, String pwd) {
        userMapper.updateUserPwdById(userId, pwd);
    }


    public UserVo findUserVoById(Long id) {
        UserRelPo userRelPo = userMapper.findUserVoById(id);
        UserVo userRelDto = new UserVo();
        CommUtil.copyProperties(userRelPo, userRelDto);
        List roleDtoList = new ArrayList<RoleDto>();
        for (RolePo rolepo : userRelPo.getRolesList()) {
            RoleDto roleDto = new RoleDto();
            CommUtil.copyProperties(rolepo, roleDto);
            roleDtoList.add(roleDto);
        }
        userRelDto.setRolesList(roleDtoList);
        return userRelDto;
    }


    public void updateUser(UserVo userVo) {
        UserPo user = new UserPo();
        try {
            CommUtil.copyProperties(userVo, user);
        } catch (Exception e) {
            logger.error("类转换异常：{}", e);
            throw new RuntimeException("类型转换异常：{}", e);
        }
        userMapper.updateUser(user);
        Long id = userVo.getId();
        List<UserRolePo> userRoles = userRoleMapper.findUserRoleByUserId(id);
        if (userRoles != null && (!userRoles.isEmpty())) {
            for (UserRolePo userRole : userRoles) {
                userRoleMapper.deleteById(userRole.getId());
            }
        }

        String[] roles = userVo.getRoleIds().split(",");
        UserRolePo userRolePo = new UserRolePo();
        for (String string : roles) {
            userRolePo.setUserId(id);
            userRolePo.setRoleId(Long.valueOf(string));
            userRoleMapper.insert(userRolePo);
        }

    }

    public void deleteUserById(Long id) {
        userMapper.deleteById(id);
        List<UserRolePo> userRoles = userRoleMapper.findUserRoleByUserId(id);
        if (userRoles != null && (!userRoles.isEmpty())) {
            for (UserRolePo userRolePo : userRoles) {
                userRoleMapper.deleteById(userRolePo.getId());
            }
        }
    }

}
